/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.tcc;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.operation.internalmgraction.TccSettingElement;
import com.inspur.edp.bef.core.tcc.builtinimpls.BefTccHandlerAssembler;
import com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.entityimpl.BefEntityImplGenCtx;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import java.util.ArrayList;
import java.util.Arrays;

import com.inspur.edp.cef.spi.jsonser.base.StringUtils;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public class CreateTccAssemblerGenerator extends ClassMethodGenerator {

  private final BefEntityImplGenCtx entityContextt;
  private final GspBizEntityObject node;

  public CreateTccAssemblerGenerator(BefEntityImplGenCtx befEntityImplContext,
      GspBizEntityObject node) {
    this.entityContextt = befEntityImplContext;
    this.node = node;
  }

  public static boolean needGenerate(GspBizEntityObject node) {
    return node.getBelongModel().isTccSupported() && !node.getTccSettings().isEmpty();
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    SuperMethodInvocation superCreateInvocation = ast.newSuperMethodInvocation();
    superCreateInvocation.setName(ast.newSimpleName(getMethodName()));
    VariableDeclarationFragment assDeclaration = ast.newVariableDeclarationFragment();
    assDeclaration.setInitializer(superCreateInvocation);
    assDeclaration.setName(ast.newSimpleName("rez"));
    VariableDeclarationStatement variableDeclarationStatement = ast
        .newVariableDeclarationStatement(assDeclaration);
    addImport(BefTccHandlerAssembler.class.getTypeName());
    variableDeclarationStatement.setType(new TypeInfo(BefTccHandlerAssembler.class).getType(ast));
    block.statements().add(variableDeclarationStatement);

    for (TccSettingElement tccSetting : node.getTccSettings()) {
      if (tccSetting.getTccAction() == null
          || StringUtils.isEmpty(tccSetting.getTccAction().getComponentId())) {
        continue;
      }
      MethodInvocation addHandlerInvocation = ast.newMethodInvocation();
      addHandlerInvocation.setName(ast.newSimpleName("addHandler"));
      addHandlerInvocation.setExpression(ast.newSimpleName("rez"));
      addHandlerInvocation.arguments()
          .add(GeneratorUtil.getStringLiteral(ast, tccSetting.getCode()));
      addHandlerInvocation.arguments()
          .add(GeneratorUtil.getStringLiteral(ast, tccSetting.getTccAction().getComponentId()));

      block.statements().add(ast.newExpressionStatement(addHandlerInvocation));
    }

    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newSimpleName("rez"));
    block.statements().add(returnStatement);

    return block;
  }

  @Override
  protected String getMethodName() {
    return "createTccAssembler";
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo(BefTccHandlerAssembler.class);
  }

  @Override
  protected ArrayList<ModifierKeyword> getAccessModifiers() {
    return new ArrayList<>(Arrays.asList(ModifierKeyword.PROTECTED_KEYWORD));
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    return null;
  }
}
